﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MiniQQ.CSharp.Sdk
{
    public class AddOn
    {
        /// <summary>
        /// 返回[@QQ号]
        /// </summary>
        /// <param name="TigObjF">At对象</param>
        /// <returns></returns>
        public static string AtQQ(string TigObjF)
        {
            return "[@" + TigObjF + "]";
        }


        /// <summary>
        /// 判断是否@了机器人
        /// </summary>
        /// <param name="RobotQQ">机器人QQ</param>
        /// <param name="Msg">判断文本</param>
        /// <returns></returns>
        public static bool IsAtRobot(string RobotQQ, string Msg)
        {
            {
                return Msg.Contains("@" + RobotQQ) || Msg.Contains("@" + Api.ApiGetNick(RobotQQ, RobotQQ));
            }
        }

        /// <summary>
        /// 判断targetQQ是否是管理员
        /// </summary>
        /// <param name="robotQQ">机器人QQ</param>
        /// <param name="groupNum">群号</param>
        /// <param name="targetQQ">目标QQ</param>
        /// <returns></returns>
        public static bool isAdmin(string robotQQ, string groupNum, string targetQQ)
        {
            if (Api.ApiGetAdminList(robotQQ, groupNum).Contains(targetQQ))
            {
                return true;
            }
            else return false;
        }
        public static byte[] String2Bytes(string str)
        {
            byte[] result = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                result[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
            }
            return result;
        }
        /// <summary>
        /// 把一句话中的所有emoji从十六进制→字符串（UTF-8）
        /// </summary>
        /// <param name="str">miniQQ返回的msg</param>
        /// <returns>转换后的msg</returns>
        public static string String2Emoji(string str)
        {
            Regex regex = new Regex(@"\[emoji=(\w+)\]");
            var ms = regex.Matches(str);
            foreach (Match m in ms)
            {
                str = str.Replace(m.Value, Encoding.UTF8.GetString(String2Bytes(m.Groups[1].Value)));
            }
            return str;
        }
    }
}
